import React, { Component } from 'react';
import { Tooltip, Input, Button, Table, message } from 'antd';
import { withRouter } from 'react-router';
import axios from 'axios';
import './index.less';
const Search = Input.Search;
const columns = [
  {
    title: '序号',
    dataIndex: 'networkId',
    width: '10%',
    key: 'networkId',
  }, {
    title: '网络中文全称',
    dataIndex: 'nickname',
    key: 'nickname',
  }, {
    title: '网络英文全称',
    dataIndex: 'networkName',
    key: 'networkName',
  }, {
    title: '网络创建者',
    width: '10%',
    dataIndex: 'userName',
    key: 'userName',
  }, {
    title: 'Peer节点数量',
    dataIndex: 'peerNum',
    width: '10%',
    key: 'peerNum',
  }, {
    title: '网络内组织数量',
    width: '10%',
    dataIndex: 'netWorkorg',
    key: 'netWorkorg',
  }, {
    title: '备注',
    width: '20%',
    dataIndex: 'remark',
    key: 'remark',
    render: (text, record) => (
      <Tooltip placement="top" title={record.remark}>
        <span className="lineClamp" style={{ 'WebkitBoxOrient': 'vertical' }} >{record.remark}</span>
      </Tooltip>
    ),
  }, {
    title: '状态',
    dataIndex: 'networkStatus',
    key: 'networkStatus',
  }
];
class MemberNetwork extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      pagination: {
        current: 1,
        defaultCurrent: 1,
        total: 70,
        onChange: this.pageChange
      },
      SearchName: ''
    };
  }
  pageChange = (page) => {
    const $this = this;
    const Token = sessionStorage.getItem('token');
    $this.state.pagination.current = page;
    axios({
      method: 'get',
      url: '/api/admin/blockchaincode/queryNetWorkListAll',
      headers: {
        Authorization: Token
      },
      params: {
        page: page,
        nickname: $this.state.SearchName
      }
    })
      .then(function (response) {
        let netData = response.data.data.rows;
        $this.state.pagination.total = response.data.data.total;
        response.data.data.rows.forEach((item, i) => {
          if (item.networkStatus === 1000) {
            netData[i].networkStatus = '创建中';
          } else if (item.networkStatus === 1010) {
            netData[i].networkStatus = '创建成功';
          } else if (item.networkStatus === 1020) {
            netData[i].networkStatus = '创建失败';
          } else if (item.networkStatus === 2000) {
            netData[i].networkStatus = '启动中';
          } else if (item.networkStatus === 2010) {
            netData[i].networkStatus = '启动成功';
          } else if (item.networkStatus === 2020) {
            netData[i].networkStatus = '启动失败';
          } else if (item.networkStatus === 2030) {
            netData[i].networkStatus = '注册失败';
          } else if (item.networkStatus === 3000) {
            netData[i].networkStatus = '停止中';
          } else if (item.networkStatus === 3010) {
            netData[i].networkStatus = '停止成功';
          } else if (item.networkStatus === 3020) {
            netData[i].networkStatus = '停止失败';
          }
        });
        $this.setState({
          data: netData
        });
      })
      .catch(function (error) {
        message.error('请求失败，请稍后重试');
      });
  }
  getLogs = () => {
    const $this = this;
    const Token = sessionStorage.getItem('token');
    $this.state.SearchName = '';
    axios({
      method: 'get',
      url: '/api/admin/blockchaincode/queryNetWorkListAll',
      headers: {
        Authorization: Token
      }
    })
      .then(function (response) {
        let netData = response.data.data.rows;
        $this.state.pagination.total = response.data.data.total;
        response.data.data.rows.forEach((item, i) => {
          if (item.networkStatus === 1000) {
            netData[i].networkStatus = '创建中';
          } else if (item.networkStatus === 1010) {
            netData[i].networkStatus = '创建成功';
          } else if (item.networkStatus === 1020) {
            netData[i].networkStatus = '创建失败';
          } else if (item.networkStatus === 2000) {
            netData[i].networkStatus = '启动中';
          } else if (item.networkStatus === 2010) {
            netData[i].networkStatus = '启动成功';
          } else if (item.networkStatus === 2020) {
            netData[i].networkStatus = '启动失败';
          } else if (item.networkStatus === 2030) {
            netData[i].networkStatus = '注册失败';
          } else if (item.networkStatus === 3000) {
            netData[i].networkStatus = '停止中';
          } else if (item.networkStatus === 3010) {
            netData[i].networkStatus = '停止成功';
          } else if (item.networkStatus === 3020) {
            netData[i].networkStatus = '停止失败';
          }
        });
        $this.setState({
          data: netData
        });
      })
      .catch(function (error) {
        console.log(error);
        message.error('请求失败，请稍后重试');
      });
  }
  // 条件搜索
  searchUser = (value) => {
    const $this = this;
    const Token = sessionStorage.getItem('token');
    $this.state.SearchName = value;
    $this.state.pagination.current = 1;
    axios({
      method: 'get',
      url: '/api/admin/blockchaincode/queryNetWorkListAll',
      headers: {
        Authorization: Token
      },
      params: {
        nickname: value
      }
    }).then(function (response) {
      let netData = response.data.data.rows;
      $this.state.pagination.total = response.data.data.total;
      response.data.data.rows.forEach((item, i) => {
        if (item.networkStatus === 1000) {
          netData[i].networkStatus = '创建中';
        } else if (item.networkStatus === 1010) {
          netData[i].networkStatus = '创建成功';
        } else if (item.networkStatus === 1020) {
          netData[i].networkStatus = '创建失败';
        } else if (item.networkStatus === 2000) {
          netData[i].networkStatus = '启动中';
        } else if (item.networkStatus === 2010) {
          netData[i].networkStatus = '启动成功';
        } else if (item.networkStatus === 2020) {
          netData[i].networkStatus = '启动失败';
        } else if (item.networkStatus === 2030) {
          netData[i].networkStatus = '注册失败';
        } else if (item.networkStatus === 3000) {
          netData[i].networkStatus = '停止中';
        } else if (item.networkStatus === 3010) {
          netData[i].networkStatus = '停止成功';
        } else if (item.networkStatus === 3020) {
          netData[i].networkStatus = '停止失败';
        }
      });
      $this.setState({
        data: netData
      });
    }).catch(function (error) {
      message.error('请求失败，请稍后重试');
    });

  }
  componentWillMount() {
    this.getLogs();

  }
  render() {
    const { match, location, history } = this.props;
    return (
      <div>
        <div className="index-buttons">
          <Search placeholder="网络名称" onSearch={value => this.searchUser(value)} enterButton size="large" style={{ width: 260 }} />
        </div>
        <Table columns={columns} dataSource={this.state.data} rowKey="networkId" bordered pagination={this.state.pagination} />
      </div>
    );
  }
}

export default withRouter(MemberNetwork);
